package com.kaifamiao.demo.d0907;

import java.sql.*;

/**
 * JDBC 的操作做一个封装
 *   只需要调用方法就可以执行操作
 * 1. 注册驱动 (驱动类会变)
 * 2. 获取连接 (url 会变)
 * 3. 执行查询
 * 4. 关闭连接
 */
public class DBUtil implements AutoCloseable{

    private static String driverClassName = "com.mysql.cj.jdbc.Driver";

    private String url;
    private String user;
    private String password;

    // 连接
    private Connection conn;
    private PreparedStatement statement;
    private ResultSet set;


    static {
        registerDriver();
    }

    private static void registerDriver() {
        registerDriver(driverClassName);
    }

    public static void registerDriver(String driverClassName) {
        try {
            Class.forName(driverClassName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.err.println("数据库驱动注册失败");
        }
    }


    public DBUtil(String url){
        this.url = url;
    }

    public DBUtil(String url, String user, String password){
        this.url = url;
        this.user = user;
        this.password = password;
    }

    public Connection getConnection() throws SQLException {
        if (user == null){
            return getConnection(this.url);
        } else {
            return getConnection(this.url, this.user, this.password);
        }
    }

    private Connection getConnection(String url) throws SQLException {
        if (conn == null){
            conn = DriverManager.getConnection(url);
        }
        return conn;
    }

    private Connection getConnection(String url, String user, String password) throws SQLException {
        if (conn == null){
            conn = DriverManager.getConnection(url, user, password);
        }
        return conn;
    }

    public ResultSet query(String sql, Object...params) throws SQLException {
        // 获取连接, 确保 conn 有值
        getConnection();
        return query(this.conn, sql, params);
    }

    public ResultSet query(Connection connection, String sql, Object...params) throws SQLException {
        // 获取连接
        if (connection != null){
            if (sql == null){
                throw new NullPointerException("SQL 不能为空");
            }
            statement = connection.prepareStatement(sql);
            // 设置参数
            for(int i = 0; i < params.length; i ++){
                statement.setObject(i + 1, params[i]);
            }

            // 执行
            set = statement.executeQuery();
            return set;
        }
        return null;
    }

    public int update(String sql, Object...params) throws SQLException {
        // 获取连接, 确保 conn 有值
        getConnection();
        return update(this.conn, sql, params);
    }

    public int update(Connection connection, String sql, Object...params) throws SQLException {
        if (connection != null){
            if (sql == null){
                throw new NullPointerException("SQL 不能为空");
            }
            statement = connection.prepareStatement(sql);
            // 设置参数
            for(int i = 0; i < params.length; i ++){
                statement.setObject(i + 1, params[i]);
            }

            // 执行
            return statement.executeUpdate();
        }
        return 0;
    }

    @Override
    public void close(){
        try {
            if (set != null){
                set.close();
                set = null;
            }
            if (statement != null){
                statement.close();
                statement = null;
            }
            if (conn != null){
                conn.close();
                conn = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
